const { getAll, getOne, getCount, add, update } = require('../../utils/dbTools');
const { getUploadedUrl } = require('../../utils/upload');
const { createLog } = require('../../utils/util');
// 数据表名称
const tableName = 'business';


// 请求数据总条数
const businessTotal = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const result = await getCount(tableName, { remove: 0 });
    if (!result) {
        output.code = 500;
        output.msg = '操作失败';
    }
    output.data = result;
    res.send(output);
}


/**
 * 获取医疗机构列表
 * 
*/
const businessList = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { page, size } = req.query;
    const params = { remove: 0 };
    if (page && size) {
        params.page = page;
        params.size = size;
    }
    const result = await getAll(tableName, params);
    if (!result) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    output.data = result;
    res.send(output);
}


/**
 * 新增患者信息
 * 
*/

const businessAdd = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    let { title, phone, legal_person, address, lnglat, status, province, city, county, introduce } = req.body;

    if (!title || !phone || !legal_person || !address || !lnglat || !status) {
        output.code = 500;
        output.msg = "参数错误， 缺少参数title, phone, legal_person, address, lnglat, status";
        return res.send(output);
    }
    const data = {
        title, phone, legal_person, province, city, county, address, lnglat, status, introduce
    }
    if (province) data.province = province;
    if (city) data.city = city;
    if (county) data.county = county;
    const { file } = req;
    if (file) {
        // 获取图片上传地址
        data.logo = getUploadedUrl(file);
    }
    const result = await add(data, tableName);
    if (!result) {
        output.code = 500;
        output.msg = '操作失败';
        return res.send(output);
    }
    createLog(req, { type: 14, remark: "新增机构(企业)" });

    res.send(output);
}

/**
 * 更新医疗机构信息
*/
const businessUpdate = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    let { id, title, phone, legal_person, address, lnglat, status, province, city, county, introduce } = req.body;
    if (!title || !phone || !legal_person || !address || !lnglat || !address || !status) {
        output.code = 500;
        output.msg = "参数错误， 缺少参数title, phone, legal_person, address, lnglat, status";
        return res.send(output);
    }
    const data = {
        title, phone, legal_person, province, city, county, address, lnglat, status, introduce, create_time: Date.now()
    }
    if (province) data.province = province;
    if (city) data.city = city;
    if (county) data.county = county;
    const { file } = req;
    if (file) {
        // 获取图片上传地址
        data.logo = getUploadedUrl(file);
    }
    const result = await update(id, data, tableName);
    if (!result) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    createLog(req, { type: 15, remark: "更新机构(企业)" });
    res.send(output);
}


/**
 *  删除患者信息
 * 
*/

const businessRemove = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { id } = req.query;
    if (!id) {
        output.code = 500;
        output.msg = "参数错误";
        return res.send(output);
    }
    // const result = await remove(id, tableName);
    // 软删除
    const result = await update(id, { remove: 1 }, tableName);
    if (!result) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    createLog(req, { type: 6, remark: "删除机构(企业)" });

    res.send(output);
}




/**
 * 获取医疗机构信息
 * 
*/
const businessInfo = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { id } = req.query;
    if (!id) {
        output.code = 500;
        output.msg = "参数错误: 缺少参数id";
        return res.send(output);
    }
    const info = await getOne(tableName, { id });
    if (info) {
        output.data = info;
    } else {
        output.code = 404;
        output.msg = "企业id错误: 未查询到相关数据";
    }
    res.send(output);
}

/**
 * 获取医生列表
 * 
*/
const doctorList = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { bus_id, page, size } = req.query;
    const params = {};
    if (page && size) {
        params.page = page;
        params.size = size;
    }
    if (!bus_id) {
        output.code = 500;
        output.msg = "参数错误: 缺少参数bus_id";
        return res.send(output);
    }
    params.bus_id = bus_id;
    const result = await getAll(tableName, params);
    if (!result) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    output.data = result;
    res.send(output);
}


module.exports = {
    businessTotal,
    businessList,
    businessAdd,
    businessUpdate,
    businessRemove,
    doctorList,
    businessInfo
}